package com.junxonline.plat.controller.household;

import com.junxonline.plat.dao.vo.*;
import com.junxonline.common.dao.vo.ResultVO;
import com.junxonline.common.util.CommonUtils;
import com.junxonline.plat.controller.base.BaseController;
import com.junxonline.plat.dao.model.Household;
import com.junxonline.plat.exception.ErrorCodeEnum;
import com.junxonline.plat.service.household.HouseholdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: HouseholdController
 * @Description: 住户控制器
 * @author JunX
 */

@RestController
@RequestMapping(value = "/admin/household")
public class HouseholdController extends BaseController {

    @Autowired
    private HouseholdService householdService;

    /**
     * 获取住户数据
     * @param vo
     * @return
     */
    @RequestMapping(value = "/getHousehold")
    public ResultVO getHousehold(@RequestBody QueryHouseholdVO vo) {

        if (CommonUtils.isEmpty(vo.getHouseholdId())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        return getReturnSuccess(householdService.getHousehold(vo));
    }

    /**
     * 筛选查询住户数据
     * @param page
     * @param vo
     * @return
     */
    @RequestMapping(value = "/queryHousehold")
    public PageResultVO queryHousehold(PageVO page, QueryHouseholdVO vo) {
        return getReturnSuccess(householdService.queryHousehold(page, vo));
    }


    /**
     * 添加住户
     * @param vo
     * @return
     */
    @RequestMapping(value = "/addHousehold")
    public ResultVO addHousehold(@RequestBody HouseholdVO vo) {

        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getHouseholdName()) || CommonUtils.isEmpty(vo.getHouseholdSex()) ||
            CommonUtils.isEmpty(vo.getHouseholdAge()) || CommonUtils.isEmpty(vo.getHouseholdIDCard())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        boolean success = householdService.addHousehold(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }

    /**
     * 修改住户
     * @param vo
     * @return
     */
    @RequestMapping(value = "/editHousehold")
    public ResultVO editHousehold(@RequestBody HouseholdVO vo) {
        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getHouseholdId()) || CommonUtils.isEmpty(vo.getHouseholdName()) ||
            CommonUtils.isEmpty(vo.getHouseholdSex()) || CommonUtils.isEmpty(vo.getHouseholdAge()) || CommonUtils.isEmpty(vo.getHouseholdIDCard())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        boolean success = householdService.editHousehold(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }


    /**
     * 删除住户
     * @param vo
     * @return
     */
    @RequestMapping(value = "/delHousehold")
    public ResultVO delHousehold(@RequestBody HouseholdIdsVO vo) {
        if (CommonUtils.isEmpty(vo)) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Long[] householdIdsArray = vo.getHouseholdIds();

        boolean successMenu = householdService.delHousehold(householdIdsArray);
        if (!successMenu) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();
    }

}


